import requests
import unittest
import os
import Utils
import parameterized

#代码驱动
class TestCodeInfo(unittest.TestCase):

    def test_user_in_user_info1(self):
        '''测试小红的用户存在'''
        url="http://127.0.0.1:5050/api/user/stu_info"
        body={"name":"小红"}
        result=requests.post(url,data=body).json()
        self.assertNotEqual(result.get("name"),None,"小红在系统中存在")

    def test_user_in_user_info2(self):
        '''测试小黑的用户存在'''
        url="http://127.0.0.1:5050/api/user/stu_info"
        body={"name":"小黑"}
        result=requests.post(url,data=body).json()
        self.assertNotEqual(result.get("name"),None,"小黑在系统中存在")

    def test_user_in_user_info3(self):
        '''测试小白的用户存在'''
        url="http://127.0.0.1:5050/api/user/stu_info"
        body={"name":"小白"}
        result=requests.post(url,data=body).json()
        self.assertNotEqual(result.get("name"),None,"小白在系统中存在")



#数据驱动
class TestdataInfo(unittest.TestCase):
    #小黑，小白，小红

    # @staticmethod
    # def get_data_by_csv(filename):
    #     data_list=[]
    #     if os.path.exists(filename):
    #         file = open(filename, "r")
    #         for line in file:
    #             data=line.strip().split(",")
    #             data_list.append(data)
    #         return data_list
    #     else:
    #         raise RuntimeError("数据源不存在，请检查%s"%filename)
        #xiaozhang,13
        #xiaowang,20
        #[[xiaozhang,13],[xiaowang,20]]

    #写死数据
    # @parameterized.parameterized.expand(
    #     [
    #         ["小红",18],
    #         ["小黑", 18],
    #         ["小白", 18]
    #     ]
    # )

    # def test_user_in_user_info1(self,name):
    #     '''测试小红的用户存在'''
    #     url = "http://127.0.0.1:5050/api/user/stu_info"
    #     body = {"name": "小红"}
    #     result = requests.post(url, data=body).json()
    #     print("result:",result)
    #     self.assertEqual(result.get("name"), None, "%s在系统中存在"%name)

    #动态获取
    @parameterized.parameterized.expand(Utils.get_data_by_csv("user.txt"))

    def test_csv_user_in_user_info(self, name, age):
        '''测试小红的用户存在'''
        url = "http://127.0.0.1:5050/api/user/stu_info"
        body = {"name": name}
        result = requests.post(url, data=body).json()
        print("result:", result, "age", age)
        self.assertNotEqual(result.get("name"), None,"%s在系统中不存在" % name)

#unittest.main()
import nnreport